{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import os\n",
    "# #fork只能用于linux/unix中\n",
    "# pid = os.fork()\n",
    "# print(\"bobby\")\n",
    "# if pid == 0:\n",
    "#   print('子进程 {} ，父进程是： {}.' .format(os.getpid(), os.getppid()))\n",
    "# else:\n",
    "#   print('我是父进程：{}.'.format(pid))\n",
    "\n",
    "\n",
    "import multiprocessing\n",
    "\n",
    "#多进程编程\n",
    "import time\n",
    "def get_html(n):\n",
    "    time.sleep(n)\n",
    "    print(\"sub_progress success\")\n",
    "    return n\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    # progress = multiprocessing.Process(target=get_html, args=(2,))\n",
    "    # print(progress.pid)\n",
    "    # progress.start()\n",
    "    # print(progress.pid)\n",
    "    # progress.join()\n",
    "    # print(\"main progress end\")\n",
    "\n",
    "    #使用线程池\n",
    "    pool = multiprocessing.Pool(multiprocessing.cpu_count())\n",
    "    # result = pool.apply_async(get_html, args=(3,))\n",
    "    #\n",
    "    # #等待所有任务完成\n",
    "    # pool.close()\n",
    "    # pool.join()\n",
    "    #\n",
    "    # print(result.get())\n",
    "\n",
    "    #imap\n",
    "    # for result in pool.imap(get_html, [1,5,3]):\n",
    "    #     print(\"{} sleep success\".format(result))\n",
    "\n",
    "    for result in pool.imap_unordered(get_html, [1,5,3]):\n",
    "        print(\"{} sleep success\".format(result))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
